package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.mapper.WorkSpaceMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public OrderOverViewVO overviewOrders() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(workSpaceMapper.countAllOrdersStatus());
        orderOverViewVO.setCancelledOrders(workSpaceMapper.countOrdersStatus(Orders.CANCELLED));
        orderOverViewVO.setCompletedOrders(workSpaceMapper.countOrdersStatus(Orders.COMPLETED));
        orderOverViewVO.setDeliveredOrders(workSpaceMapper.countOrdersStatus(Orders.CONFIRMED));
        orderOverViewVO.setWaitingOrders(workSpaceMapper.countOrdersStatus(Orders.TO_BE_CONFIRMED));
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setDiscontinued(workSpaceMapper.countDishStatus(Dish.DISCONTINUED));
        dishOverViewVO.setSold(workSpaceMapper.countDishStatus(Dish.SOLD));
        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setDiscontinued(workSpaceMapper.countSetmealStatus(Setmeal.DISCONTINUED));
        setmealOverViewVO.setSold(workSpaceMapper.countSetmealStatus(Setmeal.SOLD));
        return setmealOverViewVO;
    }

    @Override
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        //查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(begin,end,null);

        //营业额
        Double turnover = orderMapper.sumByMap(begin,end,Orders.COMPLETED);
        turnover = turnover == null ? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(begin,end,Orders.COMPLETED);

        double unitPrice = 0.0;

        double orderCompletionRate = 0.0;
        if (totalOrderCount != 0 && validOrderCount != 0) {
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userMapper.countByMap(begin,end,null);
        return new BusinessDataVO(
                turnover,
                validOrderCount,
                orderCompletionRate,
                unitPrice,
                newUsers
        );
    }
}